/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#include "TextSprite.h"

namespace liba
{
namespace lib2d
{
namespace objects
{

double TextSprite::calculate_width( const WString & text )const
{
	if( !get_rep() || !get_rep()->get_font() )
		return 0;

	double current_x = 0;
	for( int l = 0; l != text.size(); ++l )
	{
		const font::Proportional::Letter & letter = get_rep()->get_font()->get_letter( text[l] );
		current_x += letter.base_width;
	}

	return current_x;
}

void TextSprite::render( hard::Renderer * rend, const Vector2 & dst )
{
	if( !get_rep() || !get_rep()->get_font() || !get_rep()->get_image() )
	{
//		fprintf( stderr, "No font\n" );
		return;
	}

	const double base = get_rep()->get_font()->get_base();

	int current_x = 0;
	int current_y = 0;
	for( int l = 0; l != text.size(); ++l )
	{
		const font::Proportional::Letter & letter = get_rep()->get_font()->get_letter( text[l] );
		const int x = current_x - letter.base_skip;
		const int y = current_y - base;
		const int du = letter.width;
		const int dv = letter.height;
		const int u = letter.x;
		const int v = letter.y;

		rend->push_shift_add( Vector2(current_x, 0) );
		get_rep()->get_image()->blit( rend, Vector2( 0, 0 ), Rect( u, v, du, dv ) );
		rend->pop_shift();

		current_x += letter.base_width;
	}
}

} //namespace objects
} //namespace lib2d
} //namespace liba

